package core.rdd.transform;

import org.apache.spark.SparkConf;
import org.apache.spark.api.java.JavaRDD;
import org.apache.spark.api.java.JavaSparkContext;
import org.apache.spark.api.java.function.FlatMapFunction;

import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

public class Spark03_FLATMAP {
    public static void main(String[] args) {
        /**
         *map方法是将A集合变成B集合，即对A基合进行操作得到B集合
         */


        // 配置SparkConf指向你的Spark master URL
        SparkConf conf = new SparkConf()
                .setAppName("Spark03_FLATMAP") // 应用名称
                .setMaster("local[*]"); // 替换成你的master地址
        JavaSparkContext sc = new JavaSparkContext(conf);
        // 创建JavaSparkContext，它是与集群交互的主要入口点
        try {

            List<List<Integer>> stringLists = Arrays.asList(
                    Arrays.asList(1, 2, 3, 4)
                    , Arrays.asList(5, 6, 7, 8, 9, 10)
                    , Arrays.asList(15, 16, 17, 18, 19, 21));
            JavaRDD<List<Integer>> parallelize = sc.parallelize(stringLists, 2);
            /**
             * 非省略写法
             */
            JavaRDD<Integer> flatMap1 = parallelize.flatMap(new FlatMapFunction<List<Integer>, Integer>() {
                @Override
                public Iterator<Integer> call(List<Integer> list) throws Exception {
                    return list.iterator();
                }
            });


            flatMap1.collect().forEach(System.out::println);


            System.out.println("##################################分隔符#############################################");

            /**
             * Lambda
             */
            JavaRDD<Integer> flatMap2 = parallelize.flatMap(
                    list -> list.iterator()
            );



            flatMap2.collect().forEach(System.out::println);

        } finally {
            sc.close();
        }
    }
}
